﻿using System;
using System.Collections.Generic;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Kiiro.Common.Entities;
using Kiiro.Common.Utility;
using Kiiro.Model;
using Kiiro.SiteDefinition.ControlTemplates;
using Kiiro.SiteDefinition.Core;
using Kiiro.SiteDefinition.MasterPage;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

using PortalFileUrls = Kiiro.Common.Utility.Constants.PortalFileUrls;

namespace Kiiro.SiteDefinition.PortalPages
{
    public partial class EditGroup : BasePage
    {
        #region Properties
        public new User User
        {
            get;
            set;
        }

        public NavigationSub NavigationSub
        {
            get { return KiiroMaster.NavigationSub; }
        }

        public FormButtons Buttons
        {
            get { return ( FormButtons ) v2FormButtons; }
        }
        #endregion

        #region Page Events
        protected override void OnInit ( EventArgs e )
        {
            base.OnInit ( e );
            Buttons.SaveButton.ControlMode = SPControlMode.Edit;
            Buttons.CancelButton.ControlMode = SPControlMode.Edit;
            NavigationSub.DeleteButton.ControlMode = SPControlMode.Edit;
        }

        protected void Page_Load ( object sender, EventArgs e )
        {
            User = UserModel.GetOrCreateUser ( ItemId, SiteCollectionId, ServerRelativeUrl );
            ItemId = User.Id;

            SPContext context = CustomModel.GetCustomFieldsContext ( typeof ( User ), ItemId, Context, SiteCollectionId, ServerRelativeUrl );
            context.FormContext.FormMode = SPControlMode.Edit;

            Buttons.SaveButton.ItemContext = context;
            Buttons.SaveButton.RenderContext = context;

            Buttons.CancelButton.ItemContext = context;
            Buttons.CancelButton.RenderContext = context;
            Buttons.CancelButton.RedirectUrl = Cancel ( );

            NavigationSub.DeleteButton.ItemContext = context;
            NavigationSub.DeleteButton.RenderContext = context;

            litTitle.Text = "Edit Group";
            PageTitle = User.FullName;

            if ( !PermissionBitsUsers [ 0 ] || !CurrentUser.IsAdministrator )
            {
                Response.Redirect ( String.Format ( "{0}/default.aspx", ServerRelativeUrl ) );
            }
            else
            {
                AddCommandButtons ( );
                PopulateFormData ( );
            }
        }
        #endregion

        #region Methods
        private void PopulateFormData ( )
        {
            if ( !IsPostBack && User != null )
            {
                txtLogin.Text = User.Login;
                chkIsAdministrator.Checked = User.IsAdministrator;
            }
        }

        internal override bool Save ( )
        {
            if ( PermissionBitsUsers [ 0 ] )
            {
                User.IsAdministrator = chkIsAdministrator.Checked;
                User.ModifiedBy = CurrentUser;
                User.ModifiedDate = DateTime.Now;
                User.Title = "Domain Group";
                User.EditType = EditType.Updated;

                UserModel.Save ( User, SiteCollectionId, ServerRelativeUrl );

                if ( User.IsValid ( ) )
                    Response.Redirect ( Cancel ( ) );
            }
            else
            {
                Response.Redirect ( Cancel ( ) );
            }
            return true;
        }

        internal override string Cancel ( )
        {
            return String.Format ( "{0}{1}", ServerRelativeUrl, PortalFileUrls.UserAll );
        }

        internal override void Delete ( )
        {
            if ( PermissionBitsUsers [ 3 ] )
            {
                User.ModifiedBy = CurrentUser;
                User.EditType = EditType.Deleted;

                UserModel.Save ( User, SiteCollectionId, ServerRelativeUrl );

                if ( User.IsValid ( ) )
                    Response.Redirect ( Cancel ( ) );
                else
                    KiiroMaster.V2ErrorsList.ErrorMessages.Add ( FormattingHelpers.GetErrorMessageText ( User.EditTypeMessage ) );
            }
            else
                Response.Redirect ( Cancel ( ) );
        }

        private void AddCommandButtons ( )
        {
            if ( PermissionBitsUsers [ 3 ] && User.Id != CurrentUser.Id )
            {
                LinkButton btnDelete = new LinkButton ( );
                btnDelete.Text = "Delete this Group";
                btnDelete.Attributes.Add ( "class", "icon-delete" );
                string js = "javascript:{0}__doPostBack('ctl00$MainContent$V2NavigationSub$diidIODeleteItem','')";
                btnDelete.OnClientClick = string.Format ( js, "if (confirm('Are you sure that you want to delete this group?'))" );
                NavigationSub.Commands.Add ( btnDelete );
            }
        }
        #endregion
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.SiteDefinition/12/TEMPLATE/FEATURES/Kiiro.User/User/EditGroup.aspx.cs $
 * 
 * 14    2/08/10 5:04p Camerons
 * 
 * 13    2/08/10 2:28p Camerons
 * 
 * 12    1/25/10 1:17p Camerons
 * 
 * 11    1/12/10 12:03p Camerons
 * 
 * 10    1/11/10 11:51a Camerons
 * 
 * 9     1/07/10 12:28p Camerons
 * 
 * 8     1/06/10 12:39p Camerons
 * 
 * 7     26/11/09 10:35a Camerons
 * 
 * 6     17/11/09 6:46p Camerons
 * 
 * 5     27/10/09 5:35p Camerons
 * Remove IsActive from forms
 * 
 * 4     30/09/09 4:08p Camerons
 * Update all links that point to UserPages, pass User.SPUserId as the
 * ItemId so that we can safely run GetOrCreateUser
 * 
 * 3     30/09/09 2:03p Camerons
 * 
 * 2     30/09/09 1:08p Camerons
 * Implementing UserModel.GetOrCreateUser(). 
 * 
 * 1     29/09/09 5:43p Camerons
 * 
 * 1     28/09/09 3:38p Camerons
 * 
 * 20    28/09/09 10:13a Camerons
 * 
 * 19    25/09/09 1:48p Camerons
 * 
 * 18    25/09/09 9:49a Camerons
 * Updated User.IsAdministrator and User.ManageProjectsOnly to be mutually
 * exclusive
 * 
 * 17    23/09/09 4:46p Camerons
 * Fixed some old code dealing with User.ManageProjects
 * 
 * 16    21/09/09 3:30p Camerons
 * First batch of changes for 1.1.x.x - Security groups update
 * 
 * 15    9/04/09 10:27a Stefanf
 * 
 * 14    9/02/09 10:43a Stefanf
 * 
 * 13    9/01/09 11:37a Stefanf
 * 
 * 12    17/08/09 11:07a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/